import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
import {
  ShootMode,
  type Fleet,
  type Weaopn,
  Row,
  type Warship,
  ShipType,
  type SharePool,
  type GroupFleet
} from '@/coreModel/types'
import { shareTable } from '@/coreModel/shareTable'
import _ from 'lodash'

export const useFleetStore = defineStore('fleet', () => {
  const ourFleet = ref<Fleet>({ shipList: [] })
  const enemyFleet = ref<Fleet>({ shipList: [] })
  const groupedOurFleet = computed<GroupFleet>(() => {
    return ConvertFleatToGroupFleat(ourFleet.value)
  })
  const groupedEnemyFleat = computed<GroupFleet>(() => {
    return ConvertFleatToGroupFleat(enemyFleet.value)
  })
  const ourFleetSharePoolList = computed<SharePool[]>(() => {
    const sharePoolList: SharePool[] = []
    // 遍历整个舰队的所有武器
    groupedOurFleet.value.shipGroup.forEach((shipGroup) => {
      shipGroup.ship.weaponList.forEach((weapon) => {
        sharePoolList.push(CalculateWeaponShare(weapon, enemyFleet.value))
      })
    })
    return sharePoolList
  })
  return { ourFleet, enemyFleet, groupedOurFleet, groupedEnemyFleat, ourFleetSharePoolList }
})

/**
 * 将普通表示形式的舰队转换为 以 “舰船型号 + 条数” 表示的舰队
 * @param fleet 舰队
 * @returns 以 “舰船型号 + 条数” 表示的舰队
 */
export function ConvertFleatToGroupFleat(fleet: Fleet): GroupFleet {
  const groupFleet: GroupFleet = { shipGroup: [] }
  fleet.shipList.forEach((fleetShip) => {
    const findedShipGroup = groupFleet.shipGroup.find((shipGroup) => {
      return shipGroup.ship.name == fleetShip.name
    })
    if (!findedShipGroup) {
      groupFleet.shipGroup.push({
        ship: _.cloneDeep(fleetShip),
        count: 1
      })
    } else {
      findedShipGroup.count++
    }
  })
  return groupFleet
}
/**
 * 计算武器分摊池的函数
 * @param weapon 武器
 * @param fleet 敌方舰队
 * @returns 这个武器的分摊池
 */
export function CalculateWeaponShare(weapon: Weaopn, fleet: Fleet): SharePool {
  // STEP1 按照武器射击模式进行过滤
  // （如果是直射，过滤掉对手中后排所有舰船）
  let tempShipList: Warship[] = fleet.shipList
  if (weapon.shootMode == ShootMode.DirectFire) {
    tempShipList = fleet.shipList.filter((item) => {
      return item.shipTemplate.standRow == Row.Front
    })
  }
  // STEP2 设定武器锁定序列
  // STEP2.1 遍历经过上一步过滤的剩余敌方船只，找出敌方舰队中存在的，你武器锁定优先级最高级的那类船只，作为筛选条件
  let hightestAttackShipType: ShipType = ShipType.Frigate // 最高优先级的船只
  tempShipList.forEach((ship) => {
    // STEP1 计算得出当前 “hightestAttackShipType” 变量在本武器中，锁定排名是多少
    const nowHightestAttackShipTypeRank = shipTypeToWeaponAttackRank(weapon, hightestAttackShipType)
    // STEP2 计算当前遍历到的这条敌方舰船在本武器的锁定序列中，的锁定排名
    const shipRank = shipTypeToWeaponAttackRank(weapon, ship.shipTemplate.shipType)
    if (shipRank < nowHightestAttackShipTypeRank) {
      hightestAttackShipType = ship.shipTemplate.shipType
    }
  })
  // STEP2.2 过滤敌方舰队，只剩下最高锁定优先级的那个种类的船只（比如：攻击序列优先级最高的是驱逐，过滤全部066，卡里莱恩）
  tempShipList = tempShipList.filter((ship) => ship.shipTemplate.shipType == hightestAttackShipType)
  // STEP3 计算几分之几分伤（剩余舰船是阋神星*3 和斗牛*3，是6分2）
  const shareNumber = shareTable.get(tempShipList.length)
  return {
    weaponName: weapon.name,
    shareNumber: shareNumber ? shareNumber : 0,
    shipList: tempShipList
  }
}

/**
 * 输入“武器”与“舰船类型”，输出“这个舰船类型在这个武器的攻击序列中的排名”
 * @param weapon 武器
 * @param shipType 舰船类型
 * @returns 这个舰船类型在这个武器的攻击序列中的排名
 */
function shipTypeToWeaponAttackRank(weapon: Weaopn, shipType: ShipType) {
  return weapon.attackSequence.findIndex((item) => item == shipType)
}
